/*
 * Copyright 2017  lvmeng9021@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gmail.lvmeng9021.millisecondclock;

import android.os.SystemClock;
import android.util.Log;

import java.sql.Timestamp;
import java.util.Calendar;

/**
 * Created by lvmeng on 17-9-5.
 * Customize the timestamp format.
 */

public class FormatTimestamp extends Timestamp {

    private final static String TAG = "FormatTimestamp";

    private FormatTimestamp(long time) {
        super(time);
    }


    public static FormatTimestamp getInstance() {
        return new FormatTimestamp(SystemClock.currentThreadTimeMillis());
    }

    /**
     * Formats a timestamp in JDBC timestamp escape format.
     *         <code>hh:mm:ss.fffffffff</code>,
     * where <code>ffffffffff</code> indicates nanoseconds.
     * <P>
     * @return a <code>String</code> object in
     *           <code>hh:mm:ss.fffffffff</code> format
     */
    @Override
    public String toString() {
        int nanos = getNanos();
        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        int minute = Calendar.getInstance().get(Calendar.MINUTE);
        int second = Calendar.getInstance().get(Calendar.SECOND);
        String hourString;
        String minuteString;
        String secondString;
        String nanosString;
        String zeros = "000000000";
        StringBuffer timestampBuf;

        if (hour < 10) {
            hourString = "0" + hour;
            Log.d(TAG, "toString: hourString1 -- " + hourString);
        } else {
            hourString = Integer.toString(hour);
            Log.d(TAG, "toString: hourString2 -- " + hourString);
        }

        if (minute < 10) {
            minuteString = "0" + minute;
        } else {
            minuteString = Integer.toString(minute);
        }
        if (second < 10) {
            secondString = "0" + second;
        } else {
            secondString = Integer.toString(second);
        }
        if (nanos == 0) {
            nanosString = "0";
        } else {
            nanosString = Integer.toString(nanos);

            // Add leading zeros
            nanosString = zeros.substring(0, (9-nanosString.length())) +
                    nanosString;

            // Truncate trailing zeros
            char[] nanosChar = new char[nanosString.length()];
            nanosString.getChars(0, nanosString.length(), nanosChar, 0);
            int truncIndex = 8;
            while (nanosChar[truncIndex] == '0') {
                truncIndex--;
            }

            nanosString = new String(nanosChar, 0, truncIndex + 1);
        }

        // do a string buffer here instead.
        timestampBuf = new StringBuffer(20+nanosString.length());
        timestampBuf.append(hourString);
        timestampBuf.append(":");
        timestampBuf.append(minuteString);
        timestampBuf.append(":");
        timestampBuf.append(secondString);
        timestampBuf.append(".");
        timestampBuf.append(nanosString);

        return (timestampBuf.toString());
    }
}
